#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#include"2048.h"
int broad[4][4]={0};
int i = 0;int j = 0;//全局默认0

void INIT_FRIST()
{
    srand((unsigned)time(NULL));//生成两个同样的数
    int x1 = rand()%4;//0
    int y1 = rand()%4;//%25概率2 %75生成4 
    broad[x1][y1]=(rand()%4 == 0)?2:4;//(rand()%4<2)?2:4;

}
void PRINT_ALL()
{
    
    
    
   for ( i = 0; i < 4; i++)
   {
    for ( j = 0; j < 4; j++)
    {
       printf("%d\t",broad[i][j]);
    }
    
    printf("\n");
   }printf("----------------------------\n");
}
int Check_Empty()   //有无空位
{
    for ( i = 0; i < 4; i++)
    {
        for(j=0;j<4;j++)
        {
            if(broad[i][j] == 0)
            return 1;
        }
        
    }
return 0;
}



//操作完生成新数字
int NEW_ONE()//生成新数字 在0的位置
{
    if(Check_Empty()==0)
    {
        return -1 ;
    }//-1无空位，返回
    int x=0;int y=0;
    do
    {
        x = rand()%4;
        y = rand()%4;//重新赋值刷新随机数
    }
    while (broad[x][y] !=0);//找空位 运行到有空位
    
    broad[x][y] = rand()%10 == 1?4:2;   //%50概率
    return 0;
}

void LEFT()
{
    
    for ( i = 0; i < 4; i++)
    {
        int arr[4] = {0};int index=0;int temp[4]={0};
        for ( j = 0; j < 4; j++)//纵列变横行不变
        {
            if(broad[i][j] !=0)
            {
                arr[index++]=broad[i][j];//靠拢
            }
        }               
        for ( j = 0; j < 3; j++)
        {
            //printf("%d\n",broad[i][j]);
            if(arr[j]==arr[j+1] &&arr[j] !=0)
            {
            arr[j] = 2* arr[j];
            arr[j+1]=0;
            }
        }
        
         index =0;
        for(j = 0; j<4; j++)
        {
            //printf("%d\n",broad[i][j]);
           
           if (arr[j]!= 0)
           {
            temp[index] = arr[j];//把！0给temp
            index++;
           }                     
        }
        
        
        for ( j = 0; j <4; j++)
        {
            broad[i][j] = temp[j];
        }

        
    }//i
    return;
    
}

void RIGHT()//右
{
    
    for ( i = 0; i < 4; i++)
    {
        int arr[4] = {0};int index=3;int temp[4]={0};
        for ( j = 3; j >=0; j--)
        {
            if(broad[i][j] !=0)
            {
                arr[index--]=broad[i][j];//靠拢
            }
        }            
        for ( j = 3; j >=1; j--)
        {
            //printf("%d\n",broad[i][j]);
            if(arr[j]==arr[j-1] &&arr[j] !=0)
            {
            arr[j] = 2* arr[j];
            arr[j-1]=0;
            }
        }
        
         index =3;
        for(j = 3; j>=0; j--)
        {
            //printf("%d\n",broad[i][j]);
           
           if (arr[j]!= 0)
           {
            temp[index] = arr[j];//把！0给temp
            index--;
           }                     
        }             
        for ( j = 3; j >=0; j--)
        {
            broad[i][j] = temp[j];
        }

        
    }//i
    return;
    
}
void UP()
{
     for ( i = 0; i < 4; i++)
    {
        int arr[4] = {0};int index=0;int temp[4]={0};
        for ( j = 0; j < 4; j++)
        {
            //取一个纵列放进一个数组
            if(broad[j][i] !=0)
            {
                //并只取非零数，把非零数靠拢
                arr[index++]=broad[j][i];
            }
        }
                 
        for ( j = 0; j < 3; j++)
        {   //合并：靠拢后如果有两个相邻数相同数则左边的乘二，另一个赋值0
            //新的空出现了
            if(arr[j]==arr[j+1] &&arr[j] !=0)
            {
            arr[j] = 2* arr[j];
            arr[j+1]=0;
            }
        }
        
         index =0;
         //把index清零，再次靠拢用
        for(j = 0; j<4; j++)
        {
           
           //非零数靠拢在另一个数组
           if (arr[j]!= 0)
           {
            temp[index] = arr[j];//把！0给temp
            index++;
           }                     
        }
        
      //把这一整个数组赋值回原数组的纵列
        for ( j = 0; j <4; j++)
        {
            broad[j][i] = temp[j];
        }

        
    }
    return;
}
void DOWN()
{
     for ( i = 0; i < 4; i++)
    {
        int arr[4] = {0};int index=0;int temp[4]={0};
        for ( j = 3; j >=0; j--)//纵列变横行不变
        {
            if(broad[j][i] !=0)
            {
                arr[index++]=broad[j][i];//靠拢
            }
        }        
        for ( j = 0; j <4; j++)
        {
            //printf("%d\n",broad[i][j]);
            if(arr[j]==arr[j+1] &&arr[j] !=0)
            {
            arr[j] = 2* arr[j];
            arr[j+1]=0;
            }
        }
        
         index =3;
        for(j = 0; j<4; j++)
        {
            //printf("%d\n",broad[i][j]);
           
           if (arr[j]!= 0)
           {
            temp[index--] = arr[j];//把！0给temp
           }                     
        }
        
        
        for ( j = 0; j <4; j++)
        {
            broad[j][i] = temp[j];
        }

        
    }//i
    return;
}
int MAX()
{
    int max = 0;
    if(Check_Empty() != 0)
    {   
        for ( i = 0; i < 4; i++)
        {
            for ( j = 0; j < 4; j++)
            {
                if(max<broad[i][j])
                {
                    max = broad[i][j];
                }
                
            }
            
        }
        
    }
    return max;
}

int ONE_4096()
{
    if(Check_Empty()==0)
    {
        return -1 ;
    }//-1无空位，返回
    int x=0;int y=0;
    do
    {
        x = rand()%4;
        y = rand()%4;//重新赋值刷新随机数
    }
    while (broad[x][y] !=0);//找空位 运行到有空位
    
    broad[x][y] = 4096;   //%50概率
    return 0;
}